'use strict'
var eleme = require('../index')
var path = require('path')
var fs = require('fs')
const video_size_limit = 200 * 1024 * 1024
const video_ext_array = ['mp4', 'mov']

class VideoClient {
    constructor(config, token) {
        if (!config) throw error('config is required')
        this.config = config
        this.token = token;
    }

    // 上传视频（封装内容服务接口中的上传视频和获取efs配置）
    uploadVideoClient(filePath, title, desc, videoType, shopId) {
        let token = this.token
        let config = this.config

        return new Promise((resolve, reject) => {
            let video_info = new Map()

            video_info.set("title", title)
            video_info.set("description", desc)

            let video_ext = path.extname(filePath).substr(1).toLowerCase()

            if (video_ext_array.indexOf(video_ext) == -1) {
                throw new Error("只支持mp4和mov格式的视频")
            }

            fs.stat(filePath, function(err, stats) {
                if (err) {
                    throw new Error(err)
                }
                if (stats.size > video_size_limit) {
                    throw new Error("视频大小不能超过200M")
                }
                video_info.set("sizeInByte", stats.size)
                
                let rpcClient = new eleme.RpcClient(token, config);
                
                let contentService = new eleme.ContentService(rpcClient)

                contentService.getEfsConfig(videoType).then(efsConfig => {
                    fs.readFile(filePath, (err, file) => {
                        if (err) {
                            reject(err)
                        }
                        
                        console.log(file)
                        let key = shopId + '_' + Math.round(new Date().getTime()/1000)
                        video_info.set("videoKey", key)

                        let efs_client = new eleme.EfsClient(efsConfig)
                        efs_client.putObject(efsConfig.bucketName, key, file, video_ext).then(res => {
                            video_info.set("versionId", res.VersionId)
                            let contentService = new eleme.ContentService(rpcClient)
                            
                            let video_info_toJson = {
                                "videoKey": video_info.get("videoKey"),
                                "versionId": video_info.get("versionId"),
                                "sizeInByte": video_info.get("sizeInByte"),
                                "title": video_info.get("title"),
                                "description": video_info.get("description")
                            }

                            contentService.uploadVideo(video_info_toJson, shopId, videoType).then(res => {
                                    resolve(res)
                            }).catch(err => reject(err))
                        }).catch(err => reject(err))
                    })
                }).catch(err => reject(err))
            })
        })
    }
}

module.exports = VideoClient